package com.web.surf.sample.eshop.faces.backing.beans;

import java.util.List;
import com.web.surf.sample.eshop.database.manager.interfaces.ISectionService;
import com.web.surf.sample.eshop.faces.core.AbstractBean;
import com.web.surf.sample.eshop.faces.interfaces.IRedirect;
import com.web.surf.sample.eshop.utils.FacesUtil;

/**
 * Class realizing representing of sections list
 * @author SavenkoNV
 */
public class SectionBacking {

	private ISectionService sectionService;
	private List<AbstractBean> cachedCategories;
		
//	public void processCategoryID(ActionEvent ae) {
//		FacesContext context = FacesContext.getCurrentInstance();
//		Map<String, String> params = context.getExternalContext()
//				.getRequestParameterMap();		
//	FacesUtil.setSessionMapValue(IRedirect.CATEGORY_PARAM, params.get("category"));
//	}	
	
	public String getCurrentID() {
		return (String) FacesUtil.getSessionMapValue(IRedirect.REDIRECT_LINK);
	}

	public List<AbstractBean> getCategoriesList() {
		// reduction of DB tranzactions
		if (cachedCategories == null) {			
			cachedCategories = sectionService.getCategories();
		}
		return cachedCategories;
	}
		
	public List<AbstractBean> getSubcategoriesList() {
		String current = getCurrentID();
		return sectionService.getSubcategoriesByCategoryID(current);
	}
	
	//Spring
	public void setSectionService(ISectionService sectionsService) {
		this.sectionService = sectionsService;
	}	
}